package org.example.ttms.controller;

import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.StackPane;
import org.example.ttms.Main;
import org.example.ttms.customize.Pop_ups;
import org.example.ttms.entityClass.Account;
import org.example.ttms.entityClass.Listening;
import org.example.ttms.entityClass.User;
import org.example.ttms.entityClass.ticket.Ticket;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserMessageController {

    @FXML
    private StackPane stackPane3;

    @FXML
    private Button saveButton;

    @FXML
    private Button quitButton;

    @FXML
    private TextField nameTextField;

    @FXML
    private TextField passwordTextField;

    private User user;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public void initialize(){
        user = UserController.user;
        if(user.getName() != null) {
            nameTextField.setText(user.getName());
        }
        passwordTextField.setText(user.getPassword());
    }

    @FXML
    void chickQuit(ActionEvent event) {
        try {
            //关闭监听
            Listening listening = new Listening();
            listening.end();
            Main.jumpPage("login.fxml");
            Main.stage.setX(500);
            Main.stage.setY(200);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @FXML
    void chickSave(ActionEvent event) {
        HashMap<String, User> hashMap = user.obtainAllUser2();
        Pop_ups popUps = new Pop_ups();
        if(hashMap.get(nameTextField.getText()) != null && !nameTextField.getText().equals(user.getName())){
            popUps.pop_ups3("该昵称已存在");
            return ;
        }
        Account account = new Account();
        if(account.passwordIsExist(passwordTextField.getText()) && !passwordTextField.getText().equals(user.getPassword())){
            popUps.pop_ups3("该密码已存在");
            return ;
        }
        String name = user.getName();
        user.setName(nameTextField.getText());
        user.setPassword(passwordTextField.getText());
        hashMap.put(name, user);
        user.storageUser(FXCollections.observableArrayList(hashMap.values()));
        changName(name, nameTextField.getText());
        popUps.pop_ups4("保存成功");
    }
    //将该用户所有票的名字改变
    public void changName(String oldName, String newName){
        Ticket ticket = new Ticket();
        HashMap<String, Ticket> ticketHashMap = ticket.obtainAllTicket();
        for(Map.Entry<String, Ticket> map : ticketHashMap.entrySet()){
            if(map.getValue().getBuyerName() != null && map.getValue().getBuyerName().equals(oldName)){
                map.getValue().setBuyerName(newName);
            }
        }
        ticket.storageTicket(FXCollections.observableArrayList(ticketHashMap.values()));
    }
}
